package exttable.formgenerator;

import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;
import dbutils.DbUtils;
import exttable.SGTTestParametrs;
import exttable.dbclasses.AdditionalDataForVolunteers;
import utils.Localisation;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Sysadmin
 * Date: 31.10.11
 * Time: 15:26
 * To change this template use File | Settings | File Templates.
 */
public class InsertAddAdditionalDataForVolunteers {
    private JFrame frame;
    private JPanel panel;
    private JScrollPane jScrollPane;
    private JTable tableInput;
    private JButton saveButton;

    public InsertAddAdditionalDataForVolunteers(final AdditionalDataForVolunteers additionalDataForVolunteers, Vector<Vector> rowData) {
        frame = new JFrame(Localisation.getMessage("labelTableOfInputValues"));
        frame.setSize(500,550);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
        panel = new JPanel();
        Vector<String> columnName = new Vector<String>();
        columnName.add(Localisation.getMessage("table.column.prameter"));
        columnName.add(Localisation.getMessage("form.analysvalue"));
        tableInput = new JTable(rowData, columnName);
        tableInput.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        ViewDataBaseTableModel.setColumnSize(tableInput, 9);
        jScrollPane = new JScrollPane(tableInput);
        saveButton = new JButton(Localisation.getMessage("error.save"));
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (MessageFactory.isToSave()) {
                    try {
                        String query = "insert into additional_data_for_volunteers values(?,?,?,?,?,?,?,?)";
                        Connection connGeneral = DbUtils.getDBConnectionGeneral();
                        PreparedStatement psGeneral = connGeneral.prepareStatement(query);
                        psGeneral.setInt(1, additionalDataForVolunteers.getVolunteerId());
                        psGeneral.setInt(2, additionalDataForVolunteers.getDegreeOfInflammatoryId());
                        psGeneral.setInt(3, additionalDataForVolunteers.getProcessByLocalizationId());
                        psGeneral.setInt(4, StaticMethods.convertBooleanToInt(additionalDataForVolunteers.isDiseaseDurationMoreThenYear()));
                        psGeneral.setString(5, SGTTestParametrs.getInstance().getUserName());

                        JFormattedTextField dateField = new JFormattedTextField(new SimpleDateFormat("dd-MM-yyyy"));
                        dateField.setValue(new Date());
                        psGeneral.setString(6, StaticMethods.returnFormatedData(dateField.getText()));
                        psGeneral.setObject(7, null);
                        psGeneral.setObject(8, null);
                        if (psGeneral.executeUpdate() > 0) {
                            if (DbUtils.mapDataBases.get(DbUtils.SECONDARY_DATABASE_NAME) != null) {
                                Connection connSecondary = DbUtils.getDBConnectionSecondary();
                                PreparedStatement psSecondary = connSecondary.prepareStatement(query);
                                psSecondary.setInt(1, additionalDataForVolunteers.getVolunteerId());
                                psSecondary.setInt(2, additionalDataForVolunteers.getDegreeOfInflammatoryId());
                                psSecondary.setInt(3, additionalDataForVolunteers.getProcessByLocalizationId());
                                psSecondary.setInt(4, StaticMethods.convertBooleanToInt(additionalDataForVolunteers.isDiseaseDurationMoreThenYear()));
                                psSecondary.setString(5, SGTTestParametrs.getInstance().getUserName());

                                psSecondary.setString(6, StaticMethods.returnFormatedData(dateField.getText()));
                                psSecondary.setObject(7, null);
                                psSecondary.setObject(8, null);
                                psSecondary.executeUpdate();
                                connSecondary.close();
                            }

                        }
                        JOptionPane.showMessageDialog(null, Localisation.getMessage("info.dataadded"), "", JOptionPane.INFORMATION_MESSAGE);
                        frame.dispose();
                        StaticMethods.getFrameByName("FormAddAdditionalDataForVolunteers").dispose();
                        connGeneral.close();
                    } catch (MySQLIntegrityConstraintViolationException excep) {


                        JOptionPane.showMessageDialog(null, ComponentsToPanel.getDuplicateMessage(excep.getMessage()), "error",
                                JOptionPane.ERROR_MESSAGE);
                        excep.printStackTrace();
                        frame.dispose();
                    } catch (ClassNotFoundException e1) {
                        e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    } catch (SQLException e1) {
                        e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                }

            }
        });
        panel.add(jScrollPane);
        panel.add(saveButton);
        frame.add(panel);
    }
}
